package com.campus_assistant.controller;

import com.campus_assistant.common.ToVue;
import com.campus_assistant.config.db.DynamicDataSource;
import com.campus_assistant.pojo.db1.Scores;
import com.campus_assistant.pojo.db1.Student;
import com.campus_assistant.pojo.vo.StudentScores;
import com.campus_assistant.service.db1.ScoresService;
import com.campus_assistant.service.db1.StudentScoresService;
import com.campus_assistant.service.db1.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ScoresController {
    @Autowired
    private ScoresService scoresService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentScoresService studentScoresService;

    @RequestMapping("/addScores")
    public ToVue addScores(@RequestBody Scores scores){
        DynamicDataSource.dbName.set("db1");
        scoresService.addScores(scores);
        return ToVue.suc();
    }
    @RequestMapping("/deleteScores")
    public ToVue deleteScores(@RequestParam("courseName")String courseName,@RequestParam("studentName")String studentName){
        DynamicDataSource.dbName.set("db1");
        StudentScores studentScores = studentScoresService.selectStudentScoreByStudentNameAndCourseName(studentName, courseName);
        return ToVue.suc(studentScores);
    }
    @RequestMapping("/deleteScores2")
    public ToVue deleteScores2(@RequestParam("sid")int sid,@RequestParam("cid")int cid){
        DynamicDataSource.dbName.set("db1");
        scoresService.deleteScores(sid,cid);
        return ToVue.suc();
    }
    @RequestMapping("/updateScore")
    public ToVue updateScore(@RequestParam("studentName")String studentName){
        List<StudentScores> studentScores = studentScoresService.selectStudentScoreByStudentName(studentName);
        return ToVue.suc(studentScores);
    }
    @RequestMapping("/updateScore2")
    public ToVue updateScore2(@RequestBody StudentScores studentScores){
        int sId=studentScores.getSId();
        int cId=studentScores.getCId();
        Float score=studentScores.getExamResults();
        Scores scores=new Scores(sId,cId,String.valueOf(score));
        scoresService.updateScores(scores);
        return ToVue.suc();
    }
}
